package com.tanhua.app.Service;

import com.tanhua.api.UserInfoApi;
import com.tanhua.autoconfig.template.AipTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.UserInfoVo;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

@Service
public class UserInfoService {
    @DubboReference
    UserInfoApi userInfoApi;

    @Autowired
    OssTemplate ossTemplate;
    @Autowired
    AipTemplate aipTemplate;

    public void save(UserInfo userInfo){
        userInfoApi.save(userInfo);
    }
    public void updateHead(MultipartFile file,Long id) throws IOException {
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String url = ossTemplate.upload(originalFilename, inputStream);
        boolean detect = aipTemplate.detect(url);
        if (!detect) {
            throw new RuntimeException("上传图片不是人脸");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setAvatar(url);
        userInfoApi.updateById(userInfo);
    }

    public UserInfoVo findById(Long id) throws InvocationTargetException, IllegalAccessException {
        UserInfo userInfo = userInfoApi.selectById(id);

        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
      if (userInfo.getAge()!=null){
          userInfoVo.setAge(userInfo.getAge().toString());
      }
      return userInfoVo;
    }

    public void update(UserInfo userInfo){
        userInfoApi.updateById(userInfo);
    }


}
